var admin_newslist_apis = {
    list: 'http://api.fangchepingtai.com/tools/user/news/list',
    delete: 'http://api.fangchepingtai.com/tools/admin/news/delete/',
    count: 'http://api.fangchepingtai.com/tools/user/news/count',
};
(function() {
    var _page, _num;
    Path.on('/admin/__basename__.html', function(_cueent_location) {
        var qs = _cueent_location.query;
        _page = qs.get('page') || 0;
        App.set('$Cache.admin.newslist.page', _page);
        _num = qs.get('num') || 30;
        App.set('$Cache.admin.newslist.num', _num);


        coAjax.get(admin_newslist_apis.list, {
            num: _num,
            page: _page
        }, function(result) {
            App.set('$Cache.admin.newslist.list', result);
        }, function(errCode, errMsg, err) {
            alert('warn', errMsg);
        });

        coAjax.get(admin_newslist_apis.count, function(result) {
            var pages = [];
            pages.length = result ? Math.ceil(result / _num) || 1 : 0;
            App.set('$Cache.admin.newslist.pages', pages);
        }, function(errCode, errMsg, err) {
            alert('warn', errMsg);
        });
    });

    // 跳转到更新
    App.set('$Event.admin.newlist.update', function(e, vm) {
        Path.jump('/admin/news.html?_id=' + vm.get('_id'));
    });
    // 删除
    App.set('$Event.admin.newlist.delete', function(e, vm) {
        myConfirm('确定要删除资讯吗？删除后不可更改', function() {
            coAjax.delete(admin_newslist_apis.delete + vm.get('_id'), function(result) {
                console.log(result);
                alert('success', '删除成功');
                vm.remove();
            }, function(errCode, errMsg, err) {
                alert('warn', errMsg);
            });
        }, function() {})
    });
    // 翻页
    App.set("$Event.admin.newslist.prePage", function() {
        jumpPage = App.get("$Cache.admin.newslist.page") || 0;
        jumpPage > 0 ? jumpPage-- : jumpPage < 0 ? jumpPage = 0 : jumpPage;
        Path.jump("/admin/newslist.html?page=" + jumpPage);
    });
    App.set("$Event.admin.newslist.nextPage", function() {
        jumpPage = App.get("$Cache.admin.newslist.page");
        (jumpPage < App.get("$Cache.admin.newslist.pages").length - 1) ? jumpPage++ : jumpPage;
        Path.jump("/admin/newslist.html?page=" + jumpPage);
    });

}());
